'use strict';
const {
	verifyToken
} = require('wx-common');
const db = uniCloud.database();
const proCollection = db.collection('xxf-products');
const dbCmd = db.command;
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event);
	const {
		action,
		token,
		uid,
	} = event;
	if (!verifyToken(token)) {
		return {
			code: 401,
			message: 'Token无效'
		};
	}
	if (!uid) {
		return {
			msg: '用户ID参数错误！',
			code: 0,
			data: null
		};
	}
	switch (action) {
		case 'getNewList':
			return getNewList(event);
		case 'getHotList':
			return getHotList(event); // 获取产品列表
		case 'getProductList':
			return getProductList(event); // 获取产品列表
		case 'getProductDetail':
			return getProductDetail(event); // 获取产品详情
		case 'updateProductSales':
			return updateProductSales(event); // 增加销量同时减少库存
		default:
			return {
				msg: 'Invalid action！',
					code: 0,
					data: null
			};
	}
};
async function updateProductSales(params) {
	const {
		uid,
		id,
		mode,
	} = params;
	if (id) {
		const proRes = await proCollection.where({
			_id: id
		}).get();
		console.log('proRes: ', proRes);
		const updateSalesRes = await proCollection.where({
			_id: id
		}).update({
			sales_volume: proRes.data[0].sales_volume + mode,
			inventory: proRes.data[0].inventory - mode
		});
		console.log('updateSalesRes: ', updateSalesRes);
		if (updateSalesRes.updated === 1) {
			return {
				msg: '',
				code: 1,
				data: {
					...updateSalesRes
				}
			}
		} else {
			return {
				msg: '销量更新失败！',
				code: 0,
				data: null
			};
		}
	} else {
		return {
			msg: 'ID参数错误！',
			code: 0,
			data: null
		};
	}
}

async function getProductDetail(params) {
	const {
		uid,
		id,
	} = params;
	if (id) {
		const detailRes = await proCollection.where({
			_id: id
		}).get();
		console.log('detailRes: ', detailRes);
		if (detailRes && detailRes.data.length > 0) {
			const detail = detailRes.data[0];
			return {
				msg: '',
				code: 1,
				data: {
					...detail,
				}
			};
		} else {
			return {
				msg: '商品不存在！',
				code: 0,
				data: {}
			};
		}
	} else {
		return {
			msg: '商品ID错误！',
			code: 0,
			data: null
		};
	}
}

async function getProductList(params) {
	const {
		uid,
		searchStr = "",
		filterType,
		page,
		limit
	} = params;
	let proListRes = {};
	let args = {};
	const skip = (page - 1) * limit; // 计算跳过的记录数
	if (!filterType || filterType === 0) {
		proListRes = await proCollection.orderBy('sales_volume', 'desc').where({
			inventory: dbCmd.gt(0),
			name: db.RegExp({
				regexp: '.*' + searchStr + '.*', // 正则表达式，'.*'表示任意字符，'keyword'表示用户输入的关键词
				options: 'i', // 不区分大小写
			}),
		}).skip(skip).limit(limit).get();
	} else {
		proListRes = await proCollection.orderBy('_id', 'desc').where({
			type: filterType,
			inventory: dbCmd.gt(0),
			name: db.RegExp({
				regexp: '.*' + searchStr + '.*', // 正则表达式，'.*'表示任意字符，'keyword'表示用户输入的关键词
				options: 'i', // 不区分大小写
			}),
		}).skip(skip).limit(limit).get();
	}
	// proTotalRes = await proCollection.where({
	// 	...args
	// }).count();
	console.log('proListRes: ', proListRes);
	if (filterType === 0) {} else if (filterType === 1) {} else if (filterType === 2) {
		if (proListRes.data && proListRes.data.length > 0) {
			for (let i = 0; i < proListRes.data.length; i++) {
				proListRes.data[i].tag.push(`剩${proListRes.data[i].inventory}件`)
			}
		}
	}
	if (proListRes.data && proListRes.data.length > 0) {
		return {
			msg: '',
			code: 1,
			data: {
				list: proListRes.data,
				total: proListRes.data.length,
			}
		};
	} else {
		return {
			msg: '暂无数据！',
			code: 1,
			data: {
				total: 0,
				list: []
			}
		};
	}
}

async function getHotList(params) {
	const {
		uid,
	} = params;
	const proListRes = await proCollection.orderBy('sales_volume', 'desc').limit(5).get();
	console.log('proListRes: ', proListRes);
	if (proListRes.data && proListRes.data.length > 0) {
		return {
			msg: '',
			code: 1,
			data: {
				list: proListRes.data,
				total: proListRes.data.length,
			}
		};
	} else {
		return {
			msg: '暂无数据！',
			code: 1,
			data: {
				total: 0,
				list: []
			}
		};
	}
}

async function getNewList(params) {
	const {
		uid,
	} = params;
	const proListRes = await proCollection.orderBy('online_time', 'desc').limit(3).get();
	console.log('proListRes: ', proListRes);
	if (proListRes.data && proListRes.data.length > 0) {
		return {
			msg: '',
			code: 1,
			data: {
				list: proListRes.data,
				total: proListRes.data.length,
			}
		};
	} else {
		return {
			msg: '暂无数据！',
			code: 1,
			data: {
				total: 0,
				list: []
			}
		};
	}
}